#pragma once
#include<iostream>
#include<string>
#include<vector>
#include<json/json.h>
// 支持解决粘报问题，处理独立报文
#define SEP "X"
#define SEP_LEN strlen(SEP)

// 自己手写序列反序列化
#define SPACE " "
#define SPACE_LEN strlen(SPACE)
void SpliteMessage(std::string &buffer,std::vector<std::string>& outs)
{
    while(true)
    {
        int sep = buffer.find(SEP);
        if(sep == buffer.npos)
            break;
        std::string str = buffer.substr(0,sep);
        buffer.erase(0,sep + SEP_LEN);
        outs.push_back(str);
    }
}
std::string Encode(std::string& str)
{
    return str += SEP;
}
typedef struct request
{
    int _x;
    int _y;
    char _op;
    request(){}
    request(int datax,int datay,char opera)
    :_x(datax),_y(datay),_op(opera)
    {
    }
    std::string Serialize()
    {
        std::string str;
        str += std::to_string(_x);
        str += SPACE;
        str += _op;
        str += SPACE;
        str += std::to_string(_y); 
        return str;
    }
    bool Deserialized(const std::string& str)//1 + 1
    {
        size_t left = str.find(SPACE);
        if(left == str.npos)
        {
            return false;
        }
        size_t right = str.rfind(SPACE);
        if(right == str.npos || left == right)
        {
            return false;
        }
        _x = atoi(str.substr(0,left).c_str());
        _y = atoi(str.substr(right + SPACE_LEN).c_str());
        _op = str[left + SPACE_LEN];
        return true;
    }

}request_t;
typedef struct response
{

    public:
    int _code;
    int _result;//0为正常,1除0错误，-1,2程序错误
    response()
    {
        _code = -1;
        _result = 0;
    }
    response run(int x ,int y,char op)
    {
        switch(op)
        {
            case '+':
            this->_result = x + y;
            this->_code = 0;
            break;
            case '-':
            this->_result = x - y;
            this->_code = 0;
            break;
            case '*':
            this->_result = x * y;
            this->_code = 0;
            break;
            case '/':
            if(y == 0)
            {
                this->_code = 1;
                break;
            }
            this->_result = x/y;
            this->_code = 0;
            break;
            default:
            this->_code = 2;
            break;
        }
        return *this;
    }
    std::string Serialize()
    {
        std::string str;
        str += std::to_string(_result);
        str += SPACE;
        str += std::to_string(_code);
        return str;
    }
    bool Deserialized(const std::string& str) //result code:1 0
    {
        int space = str.find(SPACE);
        if(space == str.npos)
        {
            return false;
        }
        _result = atoi(str.substr(0,space).c_str());
        _code = atoi(str.substr(space + SPACE_LEN).c_str());
    }
}response_t;



// //序列化
// std::string SerializeRequest(const request_t &req)
// {
//     Json::Value root;
//     root["datax"] = req._x;
//     root["datay"] = req._y;
//     root["operator"] = req._op;
//     Json::FastWriter writer;
//     std::string json_string = writer.write(root);
//     return json_string;
// }
// //反序列化
// void DeserializeRequest(const std::string &json_string,request_t &out)
// {
//     Json::Reader reader;
//     Json::Value root;
//     reader.parse(json_string,root);
//     out._x = root["datax"].asInt();
//     out._y = root["datay"].asInt();
//     out._op = (char)root["operator"].asInt();
// }
// //序列化
// std::string SerializeResponse(const response_t &res)
// {
//     Json::Value root;
//     Json::FastWriter writer;
//     root["code"] = res._code;
//     root["result"] = res._result;
//     std::string json_string = writer.write(root);
//     return json_string;
// }
// //反序列化
// void DeSerializeResponse(const std::string &json_string,response_t &out)
// {
//     Json::Value root;
//     Json::Reader reader;
//     reader.parse(json_string,root);
//     out._code = root["code"].asInt();
//     out._result = root["result"].asInt();
// }
